#include <pthread.h>
#include <stdio.h>
#include "Future.h"

void FutureSetValue(Future_t* future, int value) {
    pthread_mutex_lock(&(future->mutex));
    future->value = value;
    future->is_ready = 1;
    pthread_cond_signal(&(future->cond));
    pthread_mutex_unlock(&(future->mutex));
}

int FutureGetValue(Future_t* future) {
    pthread_mutex_lock(&(future->mutex));
    while (!future->is_ready) {
        pthread_cond_wait(&(future->cond), &(future->mutex));
    }
    int value = future->value;
    pthread_mutex_unlock(&(future->mutex));
    return value;
}

void FutureInit(Future_t* future) {
    pthread_mutex_init(&(future->mutex), NULL);
    pthread_cond_init(&(future->cond), NULL);
    future->is_ready = 0;
}

void FutureDestroy(Future_t* future) {
    pthread_mutex_destroy(&(future->mutex));
    pthread_cond_destroy(&(future->cond));
}